import requests
from api.base_api import RegLoginApi
from api.base_api import BaseApi
from common.read_json import read_json
from common.dbuilt import clear_user_info
import unittest
from parameterized import parameterized
from common.assert_common import assert_common
import time


class TestRegLoginApi(unittest.TestCase):
    session = None

    @classmethod
    def setUpClass(cls) -> None:
        cls.session = requests.session()
        clear_user_info()

    def setUp(self) -> None:
        self.base_api = BaseApi()

    def tearDown(self) -> None:
        self.base_api.session.close()

    @classmethod
    def tearDownClass(cls) -> None:
        cls.session.close()

    @parameterized.expand(read_json('reglogin', 'img_verify_code'))
    def test01_img_verify_code(self, r, exp_status_code):
        response = self.base_api.reg_login_mode().get_img_verify_code(r)
        self.assertEqual(exp_status_code, response.status_code)

    @parameterized.expand(read_json('reglogin', 'get_phone_verify_code'))
    def test02_phone_verify_code(self, phone, imgVerifyCode, type, exp_status_code, exp_status, description):
        self.base_api.reg_login_mode().get_img_verify_code(512345845554)
        response = self.base_api.reg_login_mode().get_phone_verify_code(phone, imgVerifyCode, type)
        assert_common(self, response, exp_status_code, exp_status, description)

    @parameterized.expand(read_json('reglogin', 'user_register'))
    def test03_register(self, phone, password, verifycode, phone_code, dy_server, invite_phone, exp_status_code,
                        exp_status, description):
        self.base_api.reg_login_mode().get_img_verify_code(512345845554)
        self.base_api.reg_login_mode().get_phone_verify_code(phone, verifycode, "reg")
        response = self.base_api.reg_login_mode().user_register(phone, password, verifycode, phone_code, dy_server,
                                                                invite_phone)
        assert_common(self, response, exp_status_code, exp_status, description)

    @parameterized.expand(read_json('reglogin', 'user_login'))
    def test04_user_login(self, keywords, password, exp_status_code, exp_status, description):
        response = self.base_api.reg_login_mode().user_login(keywords, password)
        assert_common(self, response, exp_status_code, exp_status, description)

    @parameterized.expand(read_json('reglogin', 'user_login_err'))
    def test05_user_login_err(self, keywords, password, exp_status_code, exp_status, description):
        self.test_login_api = RegLoginApi(self.session)
        if password == "l123456":
            time.sleep(60)
        response = self.test_login_api.user_login(keywords, password)
        assert_common(self, response, exp_status_code, exp_status, description)
